package com.dnjn.happiness.survey.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.Tiku;
import com.dnjn.happiness.survey.domain.TikuTags;
import com.dnjn.happiness.survey.service.ITikuService;
import com.dnjn.happiness.survey.service.ITikuTagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题库类型Controller
 *
 * @author happiness
 * @date 2023-10-31
 */
@Tag(name = "surveySvyTikuTags题库类型", description = "题库类型")
@RestController
@RequestMapping("/svyTikuTags")
public class TikuTagsController extends BaseController {
    @Autowired
    private ITikuTagsService svyTikuTagsService;

    @Autowired
    private ITikuService tikuService;

    /**
     * 查询题库类型列表
     */
    @PostMapping("/list1")
    @RequiresPermissions("survey:svyTikuTags:list")
    @Log(title = "biz", businessType = BusinessType.LIST)
    public AjaxResult list1(@RequestBody TikuTags svyTikuTags, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size).addOrder(OrderItem.desc("tiku_tags_id"));
        Page<TikuTags> list = svyTikuTagsService.page(page, Wrappers.lambdaQuery(TikuTags.class)
                .like(StringUtils.isNotEmpty(svyTikuTags.getLabel()), TikuTags::getLabel, svyTikuTags.getLabel())
                .eq(TikuTags::getDeleted, "0"));
        return success(list);
    }
    /**
     * wx查询题库类型列表
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody TikuTags svyTikuTags, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<TikuTags> list = svyTikuTagsService.page(page, Wrappers.lambdaQuery(TikuTags.class)
                .like(StringUtils.isNotEmpty(svyTikuTags.getLabel()), TikuTags::getLabel, svyTikuTags.getLabel())
                .eq(TikuTags::getDeleted, "0"));
        return success(list);
    }

    /**
     * 导出题库类型列表
     */
    @Log(title = "题库类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("survey:svyTikuTags:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TikuTags svyTikuTags) {
        List<TikuTags> list = svyTikuTagsService.list(Wrappers.query(svyTikuTags));
        ExcelUtil<TikuTags> util = new ExcelUtil<TikuTags>(TikuTags.class);
        util.exportExcel(response, list, "题库类型数据");
    }

    /**
     * 获取题库类型详细信息
     */
    @GetMapping(value = "/get/{tikuTagsId}")
    @RequiresPermissions("survey:svyTikuTags:query")
    public AjaxResult get1(@PathVariable("tikuTagsId") Long tikuTagsId) {
        return success(svyTikuTagsService.getById(tikuTagsId));
    }

    /**
     * wx获取题库类型详细信息
     */
    @GetMapping(value = "/{tikuTagsId}")
    public AjaxResult get(@PathVariable("tikuTagsId") Long tikuTagsId) {
        return success(svyTikuTagsService.getById(tikuTagsId));
    }

    /**
     * 新增题库类型
     */
    @Log(title = "题库类型", businessType = BusinessType.INSERT)
    @RequiresPermissions("survey:svyTikuTags:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TikuTags svyTikuTags) {
        Assert.notNull(svyTikuTags.getLabel(), "题库label不能为空");

        svyTikuTags.setCreateBy(SecurityUtils.getUsername());
        return toAjax(svyTikuTagsService.save(svyTikuTags));
    }

    /**
     * 修改题库类型
     */
    @Log(title = "题库类型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("survey:svyTikuTags:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TikuTags svyTikuTags) {
        Assert.notNull(svyTikuTags.getLabel(), "题库label不能为空");
        return toAjax(svyTikuTagsService.updateById(svyTikuTags));
    }

    /**
     * 删除题库类型
     */
    @Log(title = "题库类型", businessType = BusinessType.DELETE)
    @RequiresPermissions("survey:svyTikuTags:remove")
    @DeleteMapping("/{tikuTagsIds}")
    public AjaxResult remove(@PathVariable Long[] tikuTagsIds) {
        for (Long tikuTagsId : tikuTagsIds) {
            Long count = tikuService.lambdaQuery().eq(Tiku::getTikuTagsId, tikuTagsId).eq(Tiku::getDeleted, "0").count();
            if (count > 0) {
                return error("请先删除关联题目记录");
            }
            svyTikuTagsService.lambdaUpdate().eq(TikuTags::getTikuTagsId, tikuTagsId).set(TikuTags::getDeleted, "1").update();
        }
        return success();
    }


    /**
     * wx获取所有图库类型
     */
    @Operation(summary = "题库类型列表-可以看到所有的题库")
    @GetMapping("/getSvyTikuTagsAll")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getSvyTikuTagsAll() {
        List<TikuTags> svyTikuTagsList = svyTikuTagsService.selectTikuTagsListByType(Constants.TIKU_TYPE_SURVEY);
        return AjaxResult.success(svyTikuTagsList);
    }
}